package cn.edu.nju.software.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

import cn.edu.nju.software.net.*;



public class StudentsManage extends JPanel implements ActionListener{
	private Connection connection;
	public ManagerOperation mo;
	private JTable studentsExists;
	private JTable studentsToDelete;
	ArrayList<StudentBean> students = new ArrayList<StudentBean>();//�Ա�ĵڼ��н��в������ǶԴ�list�ĵڼ���Ԫ�ؽ��в���
	ArrayList<StudentBean> tempStudents = new ArrayList<StudentBean>();
	ArrayList<Integer> tempForDel = new ArrayList<Integer>();//����Ҫɾ�������
	ArrayList<StudentBean> studentsToDel = new ArrayList<StudentBean>();
	
	
	ArrayList<StudentBean> studentsToAdd = new ArrayList<StudentBean>();
	ArrayList<Integer> tempForAdd = new ArrayList<Integer>();
	
	
	public StudentsManage(ManagerOperation newMO){
		super();
		mo = newMO;
		connection = Connection.getInstance();
		//init
		students = connection.handleAdministratorStudentView();// ����һ��Coursebean��ArrayList����
		
		
		for(int i =0 ;i<students.size();i++){
			tempStudents.add(students.get(i));
		}//copyһ�����������ʼ��students
		
		
		int row = students.size();
		studentsExists = new JTable();//�µ�һ���γ̱�
		studentsToDelete = new JTable();
		
		 final DefaultTableModel se_dtm  = (DefaultTableModel)studentsExists.getModel() ; 
		  studentsExists.setModel(se_dtm); 
		  final Object[] columnNames = {"username", "password", "ID",//���������final����
				   "name", "gender", "institute"};
		  se_dtm.setColumnIdentifiers(columnNames); 
		  
		  
		  //���������Ϲ�����
		  JScrollPane pane1 = new JScrollPane (studentsExists);
		  JScrollPane pane2 = new JScrollPane (studentsToDelete);
		  
		  
		  for(int i = 1;i<=row;i++){
		        
		        se_dtm.addRow(new Object[] {
		        		students.get(i-1).getUserName(),
		        		students.get(i-1).getPassword(),
		        		students.get(i-1).getId(),
		        		students.get(i-1).getName(),
		        		students.get(i-1).getGender(),
		        		students.get(i-1).getInstitute()
		        		});   
		          }//�����пγ�һ��һ����ʾ����
		  
		  
		  se_dtm.fireTableDataChanged();//repaint the table
		  
		  final DefaultTableModel std_dtm =  (DefaultTableModel)studentsToDelete.getModel() ; 
		  studentsToDelete.setModel(std_dtm);
		  std_dtm.setColumnIdentifiers(columnNames); //ǰ�������ǲ��ɱ༭��
		  
		
		  
		  
		  
		  
		 
		 //��Ӳ��ñ���  ��pannel 
		 
		
		  
		  
		  
		  
		JLabel establishedLabel = new JLabel("established students");
		JPanel establishedPanelforTable = new JPanel();//�����������пγ̵ı�
		establishedPanelforTable.add(pane1);
		
		JPanel toDeletePanel = new JPanel();
		toDeletePanel.setLayout(new GridLayout(1,2));
		JLabel toDeleteLabel = new JLabel("to Delete");
		JButton down = new JButton("down");
		down.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				int selectRow = studentsExists.getSelectedRow();
				// TODO Auto-generated method stub
				tempForDel.add(selectRow);
				studentsToDel.add(students.get(selectRow));
				std_dtm.addRow(new Object[] {
						students.get(selectRow-1).getUserName(),
		        		students.get(selectRow-1).getPassword(),
		        		students.get(selectRow-1).getId(),
		        		students.get(selectRow-1).getName(),
		        		students.get(selectRow-1).getGender(),
		        		students.get(selectRow-1).getInstitute()
		        		});
				std_dtm.fireTableDataChanged();//repaint the table
			}
			
		});
		
		
		
		toDeletePanel.add(toDeleteLabel);
		toDeletePanel.add(down);
		
		JPanel toDeletePanelforTable = new JPanel();//��������׼��ɾ��γ̵ı�
		toDeletePanelforTable.add(pane2);
		
		
		JPanel deleteButtonPanel = new JPanel();
		JButton deleteButton = new JButton("delete");
		deleteButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(connection.handleDeleteStudent(studentsToDel)){//��Ҫɾ���
					JOptionPane.showMessageDialog(null,"Delete successfully!");//�ɹ�ɾ��
					for(int i =0;i<tempForDel.size();i++){
					se_dtm.removeRow(tempForDel.get(i));//���γ̴����б����Ƴ�
					std_dtm.removeRow(i+1);//���ѡ����ʱ��
					}
					se_dtm.fireTableDataChanged();//repaint the table
					std_dtm.fireTableDataChanged();//repaint the table
					tempForDel.clear();//��ʱ�������
				}
				else{
					JOptionPane.showMessageDialog(null,"Fail to Delete!");//ɾ��ʧ��
					for(int i =0;i<tempForDel.size();i++)
						std_dtm.removeRow(i+1);//���ѡ����ʱ��
					std_dtm.fireTableDataChanged();//repaint the table
					tempForDel.clear();//��ʱ�������
				}
			}
			
		});
		deleteButtonPanel.add(deleteButton);
		
		
		
		JLabel toAddLabel = new JLabel("to Add:");
		JButton continueAddButton = new JButton("continue to add");
		JPanel PanelforLabelAndButton = new JPanel();
		PanelforLabelAndButton.add(toAddLabel);
		PanelforLabelAndButton.add(continueAddButton);
		
		final AddStudentsPanel asp = new AddStudentsPanel();//���һ�����panel
		
		
		continueAddButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(asp.checkInput()== true){//ÿһ����ݶ���ֵ��
				studentsToAdd.add(asp.getStudent());
				asp.clear();
				}
				else{
					JOptionPane.showMessageDialog(null, "Please ensure that there is no empty elements");
				}
			}
			
		});
		
		
		
		
		
		JPanel addButtonPanel = new JPanel();
		JButton addButton = new JButton("Add");
		addButtonPanel.add(addButton);
		addButton.addActionListener(new ActionListener(){
            @Override
			public void actionPerformed(ActionEvent e) {
            	if(asp.checkInput()){//˵����ǰ��ӵ����һ��Ҳ����������
            		studentsToAdd.add(asp.getStudent());
            		if(connection.handleAddStudent(studentsToAdd)){
            			JOptionPane.showMessageDialog(null, "Add successfully");
            		//ˢ          ��        ��            ʾ 
            		}
            		
            		else{
            			JOptionPane.showMessageDialog(null, "Fail to Add");
            		}
            		asp.clear();	
            			
            	}
            	else{
            		JOptionPane.showMessageDialog(null, "Please ensure that there is no empty elements");
            	}
            
				// TODO Auto-generated method stub
				
			}
			
		});
		
		GridLayout gridLayout = new GridLayout(8,0);//���panel�Ĳ���
		
		setLayout(gridLayout);
		add(establishedLabel);
		add(establishedPanelforTable);
		add(toDeletePanel);
		add(toDeletePanelforTable);
		add(deleteButtonPanel);
		add(PanelforLabelAndButton);
		add(asp);
		add(addButtonPanel);
		
		
		}


	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}
	

	

}

